home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-03 | 12.1 KB | 486 lines | [TEXT/MPS ] |
- /*************************************************************************************
- *
- * Object Oriented Shell
- *
- * Events.cp - C Source
- *
- * Copyright © Apple Computer, Inc. 1988-1993
- * All rights reserved.
- *
- * This file contains the interface between the User and the GWorldObj objects.
- * This file will call the GWorldObj class which inherits from WindowObj.
- *
- *************************************************************************************/
-
- #include "main.h"
- #include <Traps.h>
- #include "Windows.h"
- #include "WindowObj.h"
- #include "WindowObj.Link"
-
-
- /**************************************************************************************
-
- MainEventLoop
-
- Get events forever and handles them by calling Handle Event. First, call
- AdjustCursor to set our cursor shape and to set the cursor region. We
- then call WaitNextEvent() to get the event. This is OK, because we know
- we're running on System 6.0 or later by this time. If we got an event, we
- handle it by calling Handle Event(). But before doing that, we call
- AdjustCursor again in case our application had falled asleep under MultiFinder.
-
- ***************************************************************************************/
- void MainEventLoop()
- {
- RgnHandle cursorRgn, copyCursorRgn;
- Boolean gotEvent;
- EventRecord event;
- // Point mouse;
- // WindowPtr theWindow;
- long sleepTime;
- // Point mouseLoc;
-
- cursorRgn = NewRgn();
- while( !gQuit)
- {
- AppAdjustCursor( cursorRgn);
- if( gInBackground == false)
- {
- copyCursorRgn = EmptyRgn( cursorRgn) ? (RgnHandle)0 : cursorRgn;
- sleepTime = copyCursorRgn ? GetCaretTime() : 1000;
- }
- else
- {
- sleepTime = 1000;
- copyCursorRgn = (RgnHandle)nil;
- }
- sleepTime = 1;
- gotEvent = WaitNextEvent( everyEvent, &event, sleepTime, copyCursorRgn);
- AppAdjustCursor( (RgnHandle)nil);
- if( gotEvent)
- {
- HandleEvent( &event);
- }
- else
- HandleNoEvent( );
- }
- DisposeRgn( cursorRgn);
- }
-
- /**************************************************************************************
-
- HandleEvent
-
- Do the right thing for an event. Determine what kind of event it is and
- call the appropraite routines.
-
- ***************************************************************************************/
- void HandleEvent( EventRecord *event)
- {
- switch( event->what)
- {
- case mouseDown:
- HandleMouseDown( event);
- break;
- case keyDown:
- case autoKey:
- HandleKeyPress( event);
- break;
- case activateEvt:
- HandleActivate( event);
- break;
- case updateEvt:
- HandleUpdate( event);
- break;
- case diskEvt:
- HandleDiskInsert( event);
- break;
- case osEvt:
- HandleOSEvent( event);
- break;
- default:
- HandleNoEvent( );
- break;
- }
- }
-
- /**************************************************************************************
-
- HandleActivate
-
- This is called when a window is activated or deactivated. In this sample,
- the Window Manager's handling of activate and deactivate events is
- sufficeint. Other applications may have TextEdit records, controls, lists,
- etc., to activate/deactivate.
-
- ***************************************************************************************/
- void HandleActivate(EventRecord *event)
- {
- WindowPtr theWindow;
- Boolean becomingActive;
-
- theWindow = (WindowPtr) event->message;
- becomingActive = (event->modifiers & activeFlag) != 0;
- if( IsAppWindow(theWindow) )
- {
- if( IsAppWindow(theWindow))
- {
- ToWindowObj(theWindow)->DrawGrowIconObj( );
- ToWindowObj(theWindow)->ActivateObj( becomingActive);
- /* ActivateWindow(theWindow, becomingActive) */ ;
- }
- else
- {
- DrawGrowIcon( theWindow);
- if( becomingActive == true)
- {
- SelectWindow( theWindow);
- }
- }
- }
- }
-
- /**************************************************************************************
-
- HandleDiskInsert
-
- Called when we get a disk-inserted event. Check the upper word of the
- event message; if it's nonzero, then bad disk was inserted, and it needs to be formateed.
-
- ***************************************************************************************/
- void HandleDiskInsert(EventRecord *event)
- {
- WindowPtr theWindow;
- Point aPoint = {100, 100};
-
- theWindow = FrontWindow();
- if( IsAppWindow(theWindow) == true)
- ToWindowObj(theWindow)->DiskInsertObj( event);
- else
- if( HiWrd(event->message) != noErr)
- {
- (void) DIBadMount(aPoint, event->message);
- }
-
- }
-
- /**************************************************************************************
-
- HandleKeyPress
-
- The user pressed a key. What are you going to do about it?
-
- ***************************************************************************************/
- void HandleKeyPress(EventRecord *event)
- {
- char key;
- WindowPtr theWindow;
- long menuAndItem;
- WINDOWOBJ *a;
-
- key = (char) ((event->message) & charCodeMask);
- if( event->modifiers & cmdKey)
- {
- theWindow = FrontWindow();
- if( IsAppWindow(theWindow))
- {
- ToWindowObj(theWindow)->AdjustMenusObj();
- menuAndItem = MenuKey(key);
- ToWindowObj(theWindow)->MenuObj( (short)(menuAndItem >> 16),
- (short)(menuAndItem & 0xFFFF), event);
- }
- else
- {
- a = new WINDOWOBJ;
- if( a)
- {
- a->AdjustMenusObj();
- menuAndItem = MenuKey(key);
- a->MenuObj( (short)(menuAndItem >> 16),
- (short)(menuAndItem & 0xFFFF), event);
- delete a;
- }
- else
- {
- menuAndItem = MenuKey(key);
- AppMenu( (short)(menuAndItem >> 16), (short)(menuAndItem & 0xFFFF));
- }
- }
- }
- else
- {
- theWindow = FrontWindow();
- if( IsAppWindow(theWindow))
- {
- ToWindowObj(theWindow)->KeyPressObj( event);
- }
- else
- {
- a = new WINDOWOBJ;
- if( a)
- {
- a->AdjustMenusObj();
- a->KeyPressObj( event);
- delete a;
- }
- }
- /* KeyPress(event) */;
- }
- }
-
- /**************************************************************************************
-
- HandleMouseDown
-
- Called to handle mouse clicks. The user could have clicked anywhere, so
- let's first find out where by calling FindWindow. That returns a number
- indicating where in the screen the mouse was clicked. "switch" on that
- number and call the appropriate routine.
-
- ***************************************************************************************/
- void HandleMouseDown(EventRecord *event)
- {
- long newSize;
- Rect growRect;
- WindowPtr theWindow;
- short part;
- GrafPtr oldPort;
- long menuAndItem;
-
- part = FindWindow(event->where, &theWindow);
-
- switch( part)
- {
- case inMenuBar:
- theWindow = FrontWindow();
- if( IsAppWindow(theWindow))
- {
- ToWindowObj(theWindow)->AdjustMenusObj();
- menuAndItem = MenuSelect(event->where);
- ToWindowObj(theWindow)->MenuObj((short)(menuAndItem >> 16),
- (short)(menuAndItem & 0xFFFF),
- event);
- }
- else
- {
- WINDOWOBJ *a;
- a = new WINDOWOBJ;
- if( a)
- {
- a->AdjustMenusObj();
- menuAndItem = MenuSelect(event->where);
- a->MenuObj((short)(menuAndItem >> 16),
- (short)(menuAndItem & 0xFFFF),
- event);
- delete a;
- }
- }
- break;
- case inSysWindow:
- SystemClick(event, theWindow);
- break;
- case inContent:
- if( theWindow != FrontWindow() )
- {
- SelectWindow(theWindow);
- if( IsAppWindow(theWindow))
- {
- // if( ToWindowObj(theWindow)->useActivateClicks)
- // {
- // ToWindowObj(theWindow)->useActivateClicks = false;
- // HandleMouseDown( event);
- // ToWindowObj(theWindow)->useActivateClicks = true;
- // }
- }
- }
- else
- {
- if( IsAppWindow(theWindow))
- {
- GetPort( &oldPort);
- SetPort( theWindow);
- GlobalToLocal( &event->where);
- ToWindowObj(theWindow)->MouseClickObj( event);
- SetPort( oldPort);
- }
- /* ContentClick(event, theWindow */;
- }
- break;
- case inDrag:
- if( IsAppWindow(theWindow))
- ToWindowObj(theWindow)->DragWindowObj( event);
- break;
- case inGrow:
- if( IsAppWindow(theWindow))
- ToWindowObj( theWindow)->GrowWindowObj( event);
- else
- {
- growRect = qd.screenBits.bounds;
- growRect.top = growRect.left = 80; /* Arbitrary minimum size */
- newSize = GrowWindow(theWindow, event->where, &growRect);
- if( newSize != 0)
- {
- InvalidateScrollbars(theWindow);
- SizeWindow(theWindow, LoWrd(newSize), HiWrd(newSize), true);
- InvalidateScrollbars(theWindow);
- }
- }
- break;
- case inGoAway:
- if( TrackGoAway(theWindow, event->where))
- {
- CloseAnyWindow(theWindow);
- }
- break;
- case inZoomIn:
- case inZoomOut:
- if( TrackBox(theWindow, event->where, part))
- {
- SetPort(theWindow);
- EraseRect( &theWindow->portRect);
- ZoomWindow(theWindow, part, true);
- InvalRect( &theWindow->portRect);
- }
- break;
- }
- }
-
- /**************************************************************************************
-
- HandleOSEvent
-
- Deal with OSEvents (formerly, app4Events). These are messages that
- Multi-Finder-- known as the Process Manager under System 7.0 -- sends to
- us. Here, we deal with the suspend and resume message.
-
- ***************************************************************************************/
- void HandleOSEvent(EventRecord *event)
- {
- WindowPtr theWindow;
- switch ((event->message >> 24) & 0x00FF)
- {
- case suspendResumeMessage:
-
- /* In our SIZE resource, we say that we are Multi-Finder aware.
- This means that we take on the responsibility of activating
- and deactivating our own windows on suspend/resume events. */
-
- gInBackground = (event->message & resumeFlag) == 0;
- theWindow = FrontWindow();
- if(theWindow != (WindowPtr)NIL)
- {
- if( IsAppWindow(theWindow) == true)
- {
- ToWindowObj(theWindow)->DrawGrowIconObj();
- ToWindowObj(theWindow)->ActivateObj( !gInBackground);
- }
- }
- if( event->message & convertClipboardFlag )
- {
- if( theWindow != (WindowPtr)NIL)
- ToWindowObj(theWindow)->ConvertScrapObj();
- else
- AppConvertScrap();
- }
- break;
- case mouseMovedMessage:
- break;
- }
- }
-
- /**************************************************************************************
-
- HandleUpdate
-
- This is called when an update event is received for a window. It calls
- UpdateWindowto draw the contents of an application window. As an
- efficeinncy measure that does not have to be followed, it calls the draing
- routine only if the visRgn is nonempty. This will handle situation where
- calculations for drawing or drawing itself is very time consuming.
-
- ***************************************************************************************/
- void HandleUpdate(EventRecord *event)
- {
- WindowPtr theWindow = (WindowPtr)event->message;
- GrafPtr oldPort;
-
- if( IsAppWindow(theWindow) )
- {
- GetPort( &oldPort);
- SetPort( theWindow);
- BeginUpdate( theWindow);
- if( !EmptyRgn(theWindow->visRgn))
- {
- // SetPort(theWindow);
- if( IsAppWindow(theWindow))
- {
- ToWindowObj(theWindow)->UpdateObj( );
- ToWindowObj(theWindow)->DrawGrowIconObj( );
- }
- }
- EndUpdate(theWindow);
- SetPort( oldPort);
- }
- }
-
-
-
- /**************************************************************************************
-
- AppAdjustCursor
-
- The cursor needs to be adjusted. The shell's default adjustment is to set
- the cursor to an arrow. The windows that you define need to set the cursor
- to their own type of cursors.
-
- ***************************************************************************************/
- void AppAdjustCursor( RgnHandle cursorRgn)
- {
- Point mouseLoc;
- WindowPtr theWindow;
- GrafPtr oldPort;
-
- GetMouse( &mouseLoc); // These are local coords!!
- theWindow = FrontWindow();
- if( IsAppWindow(theWindow) == true)
- { /* This calls the windows adjustCursor routine */
- GetPort( &oldPort);
- SetPort( theWindow);
- GetMouse( &mouseLoc); // These are local coords!!
-
- ToWindowObj(theWindow)->AdjustCursorObj
- (mouseLoc, cursorRgn);
- SetPort( oldPort);
- }
- else
- { /* This is the application's AdjustCursorObj */
- if( cursorRgn != (RgnHandle)NIL)
- {
- SetEmptyRgn( cursorRgn);
- SetCursor( &qd.arrow);
- }
- }
- }
-
-
-
- /**************************************************************************************
-
- HandleNoEvent
-
- This will call the various idle handler's within the program.
-
- ***************************************************************************************/
- void HandleNoEvent( void)
- {
- WindowPtr theWindow;
-
- theWindow = FrontWindow();
- if( IsAppWindow(theWindow) == true)
- {
- ToWindowObj(theWindow)->IdleObj();
- }
- }
-
-